package com.fy56.platform.logistics.utils;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;

/**
 * Created by Administrator on 2015/5/14.
 */
public class IOUtils {

    /**
     * 获取Properties对象
     *
     * @param fileName
     * @return
     */
    public static Properties loadConfig(String fileName) {
        Properties properties = new Properties();
        try {
            ClassLoader classLoader = Thread.currentThread()
                    .getContextClassLoader();
            if (classLoader == null) {
                classLoader = IOUtils.class.getClassLoader();
            }
            // 把文件转换成输入流
            InputStream is = classLoader.getResourceAsStream(fileName);
            properties.load(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return properties;
    }

    /**
     * 获取Properties对象
     * @param params
     * @param fileName
     * @return
     */
    public static String getConfigParam(String params, String fileName) {
        String result = "";
        if (StringUtils.isEmpty(fileName) || StringUtils.isEmpty(params)) {
            return result;
        }
        try {
            Properties properties = loadConfig(fileName);
            result = properties.getProperty(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取所有属性（属性名，属性值）
     *
     * @param filePath
     * @return
     */
    public static Map<String, Object> getAllProperties(String filePath) {
        Map<String, Object> mapProp = Maps.newHashMap();
        Properties pros = new Properties();
        try {
            ClassLoader classLoader = Thread.currentThread()
                    .getContextClassLoader();
            if (classLoader == null) {
                classLoader = IOUtils.class.getClassLoader();
            }
            pros.load(new InputStreamReader(classLoader.getResourceAsStream(filePath), "UTF-8"));
            Enumeration keys = pros.propertyNames();
            while (keys.hasMoreElements()) {
                String key = (String) keys.nextElement();
                String value = pros.getProperty(key);
                mapProp.put(key, value);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mapProp;
    }

    /**
     * 将值写入配置文件
     */
    public static void writeProperties(String fileName, String parameterName, String parameterValue) throws IOException {
        // 本地测试特别注意，如果是maven项目，请到\target目录下查看文件，而不是源代码下
        // 注意路径不能加 / 了，加了则移除掉
        if (fileName.startsWith("/"))
            fileName.substring(1);
        String filePath = IOUtils.class.getResource("/").getPath()+fileName;
        // 获取配置文件
        Properties pps = new Properties();
        InputStream in = new BufferedInputStream(new FileInputStream(filePath));
        pps.load(in);
        in.close();
        OutputStream out = new FileOutputStream(filePath);
        // 设置配置名称和值
        pps.setProperty(parameterName, parameterValue);
        pps.store(out, "Update " + parameterName + " name");
        out.flush();
        out.close();

    }

}
